using System;
using System.Collections.Generic;
using System.Text;

namespace Structures
{
    public class Byte : Object
    {
        #region Constants
        private const int NUMBER_OF_BYTES_FOR_BYTE = 1;
        #endregion

        #region Fields
        byte? _value;
        #endregion

        #region Constructors
        public Byte(byte? value)
        { 
            _value = value;
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Converts current value to array of bytes.
        /// </summary>
        /// <returns></returns>
        public override byte[] ToByte()
        {
            byte[] buffer = new byte[NUMBER_OF_BYTES_FOR_BYTE + 1];

            if (!this.IsNull())
            {
                byte[] tmp = BitConverter.GetBytes((byte)_value);
                byte[] tmp2 = new byte[1];
                tmp2[0] = tmp[0];
                buffer[0] = 1;

                for (int i = 0; i < tmp2.Length; i++)
                {
                    buffer[i + 1] = tmp[i];
                }
            }
            else
            {
                for (int i = 0; i <= NUMBER_OF_BYTES_FOR_BYTE; i++)
                {
                    buffer[i] = 0;
                }
            }
            return buffer;
        }

        /// <summary>
        /// Converts the array of bytes to Byte value.
        /// </summary>
        /// <param name="buffer"></param>
        public override void FromByte(byte[] buffer)
        {
            if (buffer.Length < NUMBER_OF_BYTES_FOR_BYTE + 1)
                throw new ApplicationException("Cannot convert to Byte because of too short array of bytes");

            if (buffer[0] == 1)
            {
                byte[] tmp = new byte[2];
                tmp[0] = buffer[0];
                tmp[1] = 0;
                this._value = (byte)BitConverter.ToUInt16(buffer, 0);
            }
            else if (buffer[0] == 0)
                this._value = null;
            else
                throw new ApplicationException("Invalid first byte format in array of bytes");
        }

        /// <summary>
        /// If value is type of Boolean return True, else return False.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool IsNull()
        {
            if (_value.HasValue)
                return false;
            else
                return true;
        }

        /// <summary>
        /// Compare two instances of Byte values.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public int Compare(Byte value)
        {
            if (value.IsNull() && this.IsNull())
                return 0;
            else if (value.IsNull() && !this.IsNull())
                return 1;
            else if (!value.IsNull() && this.IsNull())
                return -1;
            else if (this._value > value._value)
                return 1;
            else if (this._value < value._value)
                return -1;
            else
                return 0;
        }

        /// <summary>
        /// If value is type of Byte return True, else return False.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsTypeOf(Object value)
        {
            System.Type type = value.GetType();

            if (type.FullName == "Structures.Byte")
                return true;
            else
                return false;
        }

        /// <summary>
        /// Get number of bytes needed to Byte type.
        /// </summary>
        /// <returns></returns>
        public static int GetLength()
        {
            return NUMBER_OF_BYTES_FOR_BYTE + 1;
        }

        /// <summary>
        /// Converts current value to string.
        /// </summary>
        /// <returns></returns>
        public  override string ToString()
        {
            if (!this.IsNull())
                return _value.ToString();
            else
                return string.Empty;
        }

        /// <summary>
        /// If value can be parsed to Byte type return true, else return false.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsTypeOf(string value)
        {
            byte tmp;
            return byte.TryParse(value, out tmp);
        }
        #endregion
    }
}
